package com.open.capacity.notice.processor.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.open.capacity.notice.dmo.SendMessageTask;
import com.open.capacity.notice.enums.MsgType;
import com.open.capacity.notice.processor.AbstractCreateTaskProcessor;
import com.open.capacity.notice.processor.CreateTaskProcessor;
import com.open.capacity.notice.provider.NoticeTemplateFacotoy;
import com.open.capacity.notice.provider.core.NoticeProvider;
import com.open.capacity.notice.provider.wechat.SyncWechatOperations;
import com.open.capacity.notice.provider.wechat.WechatProperties;
import com.open.capacity.notice.provider.wechat.interactive.req.LoginRequest;
import com.open.capacity.notice.provider.wechat.interactive.req.SendTextRequest;
import com.open.capacity.notice.provider.wechat.interactive.res.LoginResponse;
import com.open.capacity.notice.service.SendMessageJobService;
import com.open.capacity.notice.service.SendMessageLogService;
import com.open.capacity.notice.service.SendMessageTaskService;

import io.vavr.control.Try;
import lombok.SneakyThrows;

/**
 * 微信文本类型
 *
 */
@Service
@SuppressWarnings("all")
public class CreateWechatTextTaskProcessor extends AbstractCreateTaskProcessor implements CreateTaskProcessor {

	private static final String AUTHORIZATION = "Authorization";

	public CreateWechatTextTaskProcessor(ObjectMapper objectMapper, SendMessageTaskService sendMessageTaskService,
			SendMessageJobService sendMessageJobService, SendMessageLogService sendMessageLogService,
			TransactionTemplate transactionTemplate, NoticeTemplateFacotoy noticeTemplateFacotoy) {
		super(objectMapper, sendMessageTaskService, sendMessageJobService, sendMessageLogService, transactionTemplate,
				noticeTemplateFacotoy);
	}

	/**
	 * 构建发送微信消息
	 */
	@Override
	@SneakyThrows
	public void sendMessage(SendMessageTask task) {

		Map<String, String> extendInfo = Try.of(() -> Optional.ofNullable(task.getExtendInfo()).map(info -> {
			return JSON.parseObject(info, Map.class);
		}).orElse(new HashMap<>())).getOrElse(new HashMap<>());
		String wId = MapUtils.getString(extendInfo, "wId");
		WechatProperties wechatProperties = noticeTemplateFacotoy.getNoticeTemplate(task.getSendType())
				.<WechatProperties>getProperties();
		SyncWechatOperations syncWechatOperations = noticeTemplateFacotoy.getNoticeTemplate(task.getSendType())
				.<SyncWechatOperations>opsForChannel();
		LoginResponse loginResponse = syncWechatOperations.login(LoginRequest.builder()
				.account(wechatProperties.getUsername()).password(wechatProperties.getPassword()).build());
		if (loginResponse.isSuccess()) {
			SendTextRequest request = SendTextRequest.builder().wId(wId)
					.headers(ImmutableMap.of(AUTHORIZATION, loginResponse.getData().getAuthorization()))
					.wcId(task.getRececiver()).content(task.getSendMessage()).build();
			super.sendMessage(request, task);
		}
	}

	@Override
	public boolean supports(SendMessageTask sendMessageTask) {
		return NoticeProvider.WECHAT.getCode().equals(sendMessageTask.getSendType())
				&& MsgType.TEXT == sendMessageTask.getMsgType();
	}

}
